<script setup>
import { onMounted, ref } from 'vue'
import { getAllReportAnswerAPI, dishableReportAnswerAPI, enableReportAnswerAPI } from '@/api/user'

const allReportAnswerInfo = ref([])
const getAllReportAnswer = async () => {
  const res = await getAllReportAnswerAPI()
  allReportAnswerInfo.value = res.data
  if (allReportAnswerInfo.value.length > pageSize.value) {
    nowPageInfo.value = allReportAnswerInfo.value.slice(index.value, end.value)
  } else {
    nowPageInfo.value = allReportAnswerInfo.value
  }
}

const disableReportQuestion = async (row) => {
  const res = await dishableReportAnswerAPI(row.AnswerId)
  if (res.data === 1) {
    getAllReportAnswer()
    ElMessage.success('举报成功')
  } else {
    ElMessage.error('举报失败')
  }
}
const enableReportAnswer = async (row) => {
  const res = await enableReportAnswerAPI(row.AnswerId)
  if (res.data === 1) {
    getAllReportAnswer()
    ElMessage.success('解禁成功')
  } else {
    ElMessage.error('解禁失败')
  }
}

onMounted(() => {
  getAllReportAnswer()
})

const currentPage = ref(1)
const pageSize = ref(10)
const index = ref((currentPage.value - 1) * pageSize.value)
const end = ref(index.value + pageSize.value)
const nowPageInfo = ref([])
const handleSizeChange = (val) => {
  pageSize.value = val
  console.log(`${val} items per page`)
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  currentPage.value = val
  index.value = (currentPage.value - 1) * pageSize.value
  end.value = index.value + pageSize.value
  console.log(index.value, end.value)
  nowPageInfo.value = allReportAnswerInfo.value.slice(index.value, end.value)
}
</script>

<template>
  <el-table :data="nowPageInfo" border style="width: 100%">
    <el-table-column prop="ProblemId" label="问题id" visable="false" width="50" />
    <el-table-column prop="ProblemName" label="问题" width="250" />
    <el-table-column prop="AnswerId" label="回答id" visable="false" width="50" />
    <el-table-column prop="AnswerName" label="回答内容" width="250" />
    <el-table-column prop="FaBuUserName" label="发布人" />
    <el-table-column prop="JuBaoUserName" label="举报人" />
    <el-table-column prop="Reason" label="举报理由" width="300" />
    <el-table-column label="操作" width="200">
      <template #default="scope">
        <!-- 根据 Status 的值显示不同的按钮 -->
        <el-button
          type="danger"
          v-if="scope.row.Status === 1"
          @click="disableReportQuestion(scope.row)"
          >举报</el-button
        >
        <el-button type="success" v-else @click="enableReportAnswer(scope.row)">解禁</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    v-model:current-page="currentPage"
    v-model:page-size="pageSize"
    :page-sizes="[100, 200, 300, 400]"
    layout="sizes, prev, pager, next"
    :total="allReportAnswerInfo.length"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    background
    style="margin: 10px auto"
  />
</template>

<style></style>
